<template>
  <div class="main-container">
    <!-- <div class="btn-group">
      <el-button
        v-for="(item, index) in toggleBtnGroup"
        :key="index"
        @click="toggleView(index)"
        class="toggleBtn"
        >{{ item.value }}</el-button
      >
    </div> -->

    <div class="category-box">
      <!-- 左侧 -->
      <div class="left">
        <div class="item" v-if="showVal">
          <!-- 分类列表 -->
          <ul>
            <li
              v-for="category in categorys"
              :key="category.id"
              @click="selectCategory(category.id)"
              class="category-item"
              :class="categoryId == category.id ? 'current' : ''"
            >
              <span class="category-item-left">{{
                category.category_name
              }}</span>
              <span class="category-item-right">数目：{{ category.num }}</span>
            </li>
          </ul>
        </div>

        <div class="item" v-if="!showVal">
          <!-- 月份列表 -->
          <ul>
            <li
              v-for="(month, index) in months"
              :key="index"
              @click="selectMonth(month.year, month.month)"
              class="category-item"
            >
              <span class="category-item-left"
                >{{ month.year }} - {{ month.month }}</span
              >
              <span class="category-item-right">数目：{{ month.count }}</span>
            </li>
          </ul>
        </div>
      </div>
      <!-- 右侧 -->
      <div class="right">
        <div
          class="job-list"
          v-for="item in articles"
          :key="item.id"
          @click="handleSelectArticle(item.id)"
        >
          <div class="top-box">
            <div class="top">
              <div class="top-title">{{ item.title }}</div>
              <div class="top-money">{{ item.money }}</div>
            </div>

            <div class="middle-tag">
              <div class="tag">{{ item.address }}</div>
            </div>

            <div class="desprition">{{ item.desprition }}</div>
          </div>

          <div class="bottom">
            <div class="bottom-left">
              <img :src="item.cover_picture" alt="" class="circle-img" />
              <div class="conpanyName">{{ item.conpanyName }}</div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import LayoutCol from "@/components/LayoutCol.vue";
import ViewCover from "@/components/ViewCover.vue";
import LightText from "@/components/lightText.vue";
export default {
  name: "HomeViewCategory",
  components: { LayoutCol, ViewCover, LightText },
  created() {
    // if (this.$route.query.id) {
    //   this.categoryId = this.$route.query.id;
    // }
    this.getCategorys();
    this.getArticlesByCategory();
    this.getMonths();
    this.getArticlesByMonth();
    this.selectCategory(1);
  },
  data() {
    return {
      // 按分类
      categorys: [],
      articles: [],
      categoryId: 1,
      showVal: true,

      // 按时间
      months: [],
      articles: [],
      year: 2022,
      month: 5,

      toggleBtnGroup: [{ value: "按职位类别" }, { value: "按发布时间" }],
    };
  },
  methods: {
    // 获取所有分类
    getCategorys() {
      this.$http.get(`/categorys`).then((res) => {
        const { code, msg, result } = res;
        if (code == 0) {
          this.categorys = result;
        }
      });
    },

    // 根据分类获取职位列表
    getArticlesByCategory() {
      this.$http.get(`/articles?category_id=${this.categoryId}`).then((res) => {
        const { code, msg, result } = res;
        if (code == 0) {
          this.articles = result;
        }
      });
    },

    // 选择某个分类
    selectCategory(id) {
      this.categoryId = id;
      this.getArticlesByCategory();
    },

    // 选择某篇职位
    handleSelectArticle(id) {
      this.$router.push(`/article?id=${id}`);
    },

    // 获取所有月份
    getMonths() {
      this.$http.get(`/articles?type=month_stat`).then((res) => {
        const { code, msg, result } = res;
        if (code == 0) {
          this.months = result;
        }
      });
    },
    // 根据月份获取职位列表
    getArticlesByMonth() {
      this.$http
        .get(`/articles?year=${this.year}&month=${this.month}`)
        .then((res) => {
          // console.log('months-articles-res', res)
          const { code, msg, result } = res;
          if (code == 0) {
            this.articles = result;
          }
        });
    },
    // 选择某个分类
    selectMonth(year, month) {
      this.year = year;
      this.month = month;
      this.getArticlesByMonth();
    },
    // 选择某篇职位
    handleSelectArticle(id) {
      this.$router.push(`/article?id=${id}`);
    },
    toggleView(index) {
      index == 0 ? (this.showVal = true) : (this.showVal = false);
    },
  },
};
</script>

<style lang="less" scoped>
.main-container {
  padding-top: 200px;
  background-color: #f5f7fa;
}

.btn-group {
  margin-left: 45%;
  margin-top: 20px;
}

.main {
  background-color: #f5f7fa;
}

.category-item {
  height: 40px;
  padding: 10px 20px;
  cursor: pointer;
  transition: all 0.1s ease-out;
  font-size: 16px;

  .category-item-left {
    float: left;
  }

  .category-item-right {
    float: right;
  }

  &:hover {
    color: #04a0ea;
  }
}

.current {
  color: #04a0ea;
}

.item {
  width: 400px;
  background-color: #ffffff;
  box-shadow: rgba(0, 0, 0, 0.24) 0px 3px 8px;
  border-radius: 10px;
  margin-right: 20px;
}

.toggleBtn {
  font-size: 20px;
}

.category-box {
  width: 70%;
  margin: 20px auto;
  display: flex;
}

.job-list {
  width: 800px;
  background-color: #ffffff;
  height: 150px;
  border-radius: 15px;
  margin-bottom: 15px;
  cursor: pointer;
  position: relative;
  overflow: hidden;

  .top-box {
    padding: 15px;
    .top {
      width: 100%;
      display: flex;
      justify-content: space-between;

      .top-title {
        color: #222;
      }
      .top-money {
        color: #fe574a;
        font-size: 18px;
        font-weight: bold;
      }
    }

    .tag {
      width: 60px;
      text-align: center;
      background-color: #f8f8f8;
      padding: 5px;
      border-radius: 5px;
      margin: 10px 0px;
      font-size: 14px;
    }
    .desprition {
      font-size: 14px;
    }
  }

  .bottom {
    display: flex;
    background: linear-gradient(90deg, #cbeafd 0, #fcfbfa 100%);
    height: 40px;
    position: absolute;
    bottom: 0px;
    width: 100%;
    .bottom-left {
      display: flex;
      justify-content: center;
      vertical-align: middle;
      align-items: center;
      .conpanyName {
        font-size: 15px;
        margin-left: 10px;
      }
    }
    .circle-img {
      width: 30px;
      height: 30px;
      border-radius: 15px;
      margin-left: 20px;
    }
  }
}
</style>
